import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_demo/widget/buttons.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'other/application.dart';

class EditSocialCreditCodeScreen extends StatelessWidget {

  final GlobalKey _formKey = GlobalKey<FormState>();
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {

    var et_old_account = Padding(
      padding: const EdgeInsets.fromLTRB(64, 32, 64, 32),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: TextFormField(
          autofocus: true,
          decoration: const InputDecoration(
            labelText: '旧的社会信用代码',
            hintText: '您的旧的社会信用代码',
            border: InputBorder.none,
          ),
          validator: (value) {
            if (value!.trim().isEmpty) {
              return '旧的社会信用代码不能为空';
            } else if (value.trim().length > 18) {
              return '旧的社会信用代码超出最大长度';
            } else {
              return null;
            }
          },
        ),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black26, width: 1.0),
          borderRadius: BorderRadius.circular(5.0),
        ),
      ),
    );

    var et_new_account = Padding(
      padding: const EdgeInsets.fromLTRB(64, 0, 64, 32),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: TextFormField(
          controller: _controller,
          autofocus: true,
          decoration: const InputDecoration(
            labelText: '新的社会信用代码',
            hintText: '您的新的社会信用代码',
            border: InputBorder.none,
          ),
          validator: (value) {
            if (value!.trim().isEmpty) {
              return '新的社会信用代码不能为空';
            } else if (value.trim().length > 18) {
              return '新的社会信用代码超出最大长度';
            } else {
              return null;
            }
          },
        ),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black26, width: 1.0),
          borderRadius: BorderRadius.circular(5.0),
        ),
      ),
    );

    var et_pid = Padding(
      padding: const EdgeInsets.fromLTRB(64, 0, 64, 32),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: TextFormField(
          autofocus: true,
          decoration: const InputDecoration(
            labelText: '法定代表人身份证号',
            hintText: '您的法定代表人身份证号',
            border: InputBorder.none,
          ),
          validator: (value) {
            if (value!.trim().isEmpty) {
              return '法定代表人身份证号不能为空';
            } else if (value.trim().length > 19) {
              return '法定代表人身份证号超出最大长度';
            } else {
              return null;
            }
          },
        ),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black26, width: 1.0),
          borderRadius: BorderRadius.circular(5.0),
        ),
      ),
    );

    var bt_commit = Padding(
      padding: EdgeInsets.fromLTRB(64, 0, 64, 32),
      child: BaseButton(
        radius: 12,
        onTap: () {
          if ((_formKey.currentState as FormState).validate())
            validatePassed(context);
        },
        child: Text(
          '完成',
          style: TextStyle(
            fontSize: 16,
            color: Colors.white,
          ),
        ),
      ),
    );

    return Scaffold(
      appBar: AppBar(
        title: const Text('修改社会信用代码'),
      ),
      body: Form(
        key: _formKey,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            et_old_account,
            et_new_account,
            et_pid,
            bt_commit,
          ],
        ),
      ),
    );
  }

  void validatePassed(BuildContext context) async {

    companyInfoData['社会统一信用代码'] = _controller.text;

    Timer(Duration(milliseconds: 500), (){
      Fluttertoast.showToast(msg: '提交成功');
      Navigator.pop(context);
    });
  }
}